package com.gps.misc;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.appengine.utils.AeLoadingCenter;
import com.appengine.utils.GsonUtils;
import com.appengine.utils.SavingAndLoadingCenter;
import com.appengine.utils.UserUtils;
import com.datatypes.ActivityTracks;
import com.datatypes.FriendsZone;
import com.datatypes.Log;
import com.datatypes.OfflineLogs;
import com.datatypes.Track;
import com.datatypes.User;
import com.datatypes.listItemData;
import com.gps.track.GpsShowMap;
import com.gps.track.GpsTrackingActivityGhost;
import com.sporteamws.R;
import com.sporteamws.SharedPref;

public class ActivityRecord extends Activity implements OnItemClickListener,
		OnCreateContextMenuListener {

	ArrayList<listItemData> listVals = null;
	User user = null;
	ActivityTracks activityTracks = null;
	com.datatypes.Activity activity = null;
	String title = "";
	int position = 0;
	EditText newName = null;
	private ListView list;
	private ActivityList adapter;
	boolean returnHere = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		newName = new EditText(this);
		listVals = new ArrayList<listItemData>();
		String UserStr = getIntent().getExtras().getString("USER");

		user = GsonUtils.ConvertGsonStringToUser(UserStr);
		activityTracks = SavingAndLoadingCenter.loadActivityTracks(
				user.GetFacebookID(), this);
		for (com.datatypes.Activity a : activityTracks.getActivityList()) {
			if (a.isImTheSender(user.GetFacebookID())) {
				if (!a.isClosedActivity()) { 
						listVals.add(new listItemData(a));
				}
			} else {
				if (a.getReceiverTrack() == null)
					listVals.add(new listItemData(a));
			}
		}

		list = (ListView) findViewById(R.id.list);
		list.setSelector(R.layout.list_selector_activity);
		adapter = new ActivityList(this, listVals);
		list.setAdapter(adapter);
		list.setOnItemClickListener(this);
		list.setOnCreateContextMenuListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// if (!returnHere)
		// finish();
	}

	public void onItemClick(AdapterView<?> l, View v, int position, long id) {

		Bundle bundle = new Bundle();
		activity = listVals.get(position).getActivity();
		Intent intent = null;
		Track track = null;
		if (activity.isImTheSender(user.GetFacebookID())) {
			if (activity.getReceiverTrack() == null)
				track = activity.getSenderTrack();
			else {
				track = activity.getReceiverTrack();
				activity.setClosedActivity(true);
				/*
				 * boolean friendInAct = false; for (com.datatypes.Activity a :
				 * activityTracks .getActivityList()) { if
				 * (a.getSenderID().equals(activity.getReceiverID())) {
				 * friendInAct = true; break; } }
				 */
				// if (!friendInAct) {
				FriendsZone fz = SavingAndLoadingCenter.loadFriendsZone(
						user.GetFacebookID(), this);
				fz.getFriend(activity.getReceiverID()).setInActivity(false);
				SavingAndLoadingCenter.saveFriendsZone(user.GetFacebookID(),
						fz, this);
				// }
				// UserUtils.UpdateActivityClosed(user.GetFacebookID(),
				// activity.getActivity_id(), true);
				// UserUtils.SendActivityToUser(activity);
				// AeLoadingCenter.SendActivity(activity, this);
				AeLoadingCenter.SendActivityOnBackground(activity, this);
				SavingAndLoadingCenter.saveActivityTracks(user.GetFacebookID(),
						activityTracks, this);
			}

			intent = new Intent(this, GpsShowMap.class);

		} else {
			bundle.putBoolean("IGHOST", false);
			bundle.putString("ACTIVITY_ID", activity.getActivity_id());
			intent = new Intent(this, GpsTrackingActivityGhost.class);
			track = activity.getSenderTrack();

		}
		String trackStr = GsonUtils.ConvertTrackToGsonString(track);
		bundle.putString("GHOST_TRACK", trackStr);
		bundle.putBoolean("IGHOST", false);
		bundle.putString("USER", getIntent().getExtras().getString("USER"));
		intent.putExtras(bundle);
		startActivity(intent);

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

		activity = listVals.get(info.position).getActivity();

		if (!activity.isImTheSender(user.GetFacebookID())) {

			title = "show track";

			position = info.position;
			menu.setHeaderTitle(title);
			menu.add("Show track");
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		String itemName = "" + item.getTitle();
		if (itemName.equals("Show track")) {
			Intent intent = new Intent(this, GpsShowMap.class);
			Bundle bundle = new Bundle();
			String trackStr = GsonUtils.ConvertTrackToGsonString(activity
					.getSenderTrack());
			bundle.putString("GHOST_TRACK", trackStr);
			bundle.putString("USER", getIntent().getExtras().getString("USER"));
			intent.putExtras(bundle);

			returnHere = true;
			SharedPref.returnToMainMenu = false;
			startActivity(intent);
		} else
			return false;

		return super.onContextItemSelected(item);
	}
}
